/*
 * 
 * 
 * 
 */
package com.exp.sys.bean.shiro;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;


/**
 * 权限认证
 * 
 * 
 * 
 */
public class AuthenticationRealm extends AuthorizingRealm {


	/**
	 * 获取认证信息
	 * 
	 * @param token
	 *            令牌
	 * @return 认证信息
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) {
		AuthenticationToken authenticationToken = (AuthenticationToken) token;
		String username = authenticationToken.getUsername();
		String password = new String(authenticationToken.getPassword());
		String captchaId = authenticationToken.getCaptchaId();
		String captcha = authenticationToken.getCaptcha();
		String ip = authenticationToken.getHost();

/*		if (username != null && password != null) {
			Example example = new Example(Admin.class);
			example.createCriteria().andEqualTo("username", username);
			Admin admin = adminDomain.find(example);
			if (admin == null) {
				throw new UnknownAccountException();
			}

			if (!DigestUtils.md5Hex(password).equals(admin.getPassword())) {
//				int loginFailureCount = admin.getLoginFailureCount() + 1;
//				if (loginFailureCount >= setting.getAccountLockCount()) {
//					admin.setIsLocked(true);
//					admin.setLockedDate(new Date());
//				}
//				admin.setLoginFailureCount(loginFailureCount);
//				adminService.update(admin);
				throw new IncorrectCredentialsException();
			}
			return new SimpleAuthenticationInfo(admin, password, getName());
		}
*/		throw new UnknownAccountException();
	}

	/**
	 * 获取授权信息
	 * 
	 * @param principals
	 *            principals
	 * @return 授权信息
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
/*		Admin principal = (Admin) principals.getPrimaryPrincipal();
		if (principal != null) {
//			List<String> authorities = adminService.findAuthorities(principal.getId());
//			if (authorities != null) {
				SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//				authorizationInfo.addStringPermissions(authorities);
				return authorizationInfo;
//			}
		}
*/		return null;
	}

}